//保存表格的实例对象
var sx = null;
//调用封装方法
getTable();


// ---------------------------------------------------
//方法区

//封装方法重载表格
function sxTable(){
    //通过表格id参数  重载刷新
    sx.reload()
}


//封装渲染表格
function getTable(){
    layui.use(['table','layer','jquery','form','laydate'],function() {
        //模板保存到变量上
        var laydate = layui.laydate;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.$;

        //监听顶部刷新按钮
        form.on('submit(sx)',function (d){
            location.reload();
        })

        //鉴定顶部返回按钮
        form.on('submit(fh)',function (d){
            layer.msg('返回上一级页面')
        })


        //监听顶部搜索按钮
        form.on('submit(*)',function (d){
            console.log(d.field)
            var data = d.field;
            sx.reload({
                url:'/YltUser/UserManage/getUserSealByLike',
                where:{
                    userName:data.userName,
                    userAccount:data.userAccount,
                    sealTime:data.sealTime
                }
            },true)
            return false
        })

        //监听顶部重置按钮
        form.on('submit(cz)',function (d){
            $("[name=userName]").val(undefined)
            $("[name=userAccount]").val(undefined)
            $("[name=sealTime]").val(undefined)

            sx.reload({
                url:'/YltUser/UserManage/getUserSeal',
            },true)
            return false
        })

        // 渲染input日期
        laydate.render({
            elem:'#sealTime'
        })

        sx = table.render({
            elem:"#demo",
            url:"/YltUser/UserManage/getUserSeal",
            page:{
                layout:['count','prev','page','next','limit'],
                groups:5,
                limit:10,
                limits:[5,10,20]
            },
            cols:
                [[
                    // {field: 'seal_id', title: '序号',width: 100,align: 'center'},
                    {field: 'insider', title: '用户类型',width: 100,align: 'center',templet:function (d){
                            if(d.insider == '2'){
                                return '<i class="layui-icon layui-icon-group" style="font-size: 30px; color: #FF5722;"></i>  '
                            }else if(d.insider == '1') {
                                return '<i class="layui-icon layui-icon-group" style="font-size: 30px; color: #01AAED;"></i>  '
                            }else{
                                return '<i class="layui-icon layui-icon-group" style="font-size: 30px; color: #314644;"></i>  '
                            }
                        }},
                    {field: 'user_id', title: '用户编号',width: 150,align: 'center'},
                    {field: 'user_account', title: '用户账号',width: 150,align: 'center'},
                    {field: 'user_name', title: '用户昵称',width: 150,align: 'center'},
                    {field: 'seal_time', title: '封停开始时间', width:200,align: 'center'},
                    {field: 'deblocking_time', title: '封停结束时间',width: 200,align: 'center'},
                    {field: 'seal_day', title: '封停天数', width:150,align: 'center',templet:function (d){
                            return d.seal_day +'天';
                        }},
                    {field: 'seal_remark', title: '封停备注', width: 150, align: 'center'},
                    {title: '操作',toolbar:'#tooldemo',width: 150,align: 'center'}
                ]],
            toolbar:'#toolbardemo',
            skin: 'line',
            parseData:function (res){
                return{
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
            }

        }),

        //监听表格工具栏
        table.on('tool(filterDemo)',function(obj){
            var data = obj.data;
            if(obj.event == 'jf'){
                console.log(data)
                layer.confirm('确定要解封该用户账号吗？',function (){
                    //发送ajax
                    $.ajax({
                        url:'/YltUser/UserManage/upUserSeal',
                        type:'post',
                        data:{sealId:data.seal_id,sealState:1},
                        dataType:'json',
                        success:function (res){
                            if (res.state == '200'){
                                layer.msg('解封账户完毕',{time:1000,icon:6},function (index){
                                    layer.close(index);
                                    sxTable();
                                })
                            }else {
                                layer.msg("解封账户失败",{time: 1500,icon:5},function (index){
                                    layer.close(index);
                                });
                            }
                        },
                        errors:function (e){
                            console.log(e);
                            console.log("请求失败")
                        }
                    })
                })
            }
        })
    })
}



